<template>
  <div class="form__wrapper">
    <base-form
      ref="form"
      :label-width="'110px'"
      column="single"
      :form-list="formList"
      :form-rules="formRules"
      :form-disabled="submitLoading" />
    <div class="actions">
      <el-button type="primary" size="small" @click="submit" :loading="submitLoading">提交</el-button>
      <el-button type="default" size="small" @click="cancel" :disabled="submitLoading">取消</el-button>
    </div>
  </div>
</template>

<script>
import BaseForm from '@/components/BaseForm'

import { dict_productionType } from '@/utils/dictionary'
import { createApi } from '@/api/modules/production'

export default {
  name: 'ProductionCreate',
  components: { BaseForm },
  data () {
    return {
      formList: [
        { label: '产品编号', prop: 'code', placeholder: '请输入（创建后不可修改）' },
        { label: '产品名称', prop: 'name' },
        { label: '产品类型', prop: 'type', type: 'select', options: dict_productionType },
        { label: '接口数量', prop: 'interfaceNum', type: 'number', min: 1, max: 20 },
      ],

      formRules: {
        code: [
          { required: true, message: '产品编号不能为空', trigger: ['blur', 'change'] },
          { max: 50, message: '产品编号不能超过50个字符', trigger: ['blur', 'change'] }
        ],
        name: [
          { required: true, message: '产品名称不能为空', trigger: ['blur', 'change'] },
          { max: 50, message: '产品名称不能超过50个字符', trigger: ['blur', 'change'] }
        ],
        type: [
          { required: true, message: '产品类型不能为空', trigger: ['blur', 'change'], type: 'number' },
        ],
        interfaceNum: [
          { required: true, message: '接口数量不能为空', trigger: ['blur', 'change'], type: 'number' },
        ],
      },

      submitLoading: false
    }
  },
  methods: {
    submit () {
      
      this.$refs['form'].validate(async valid => {
        if(valid) {
          try {
            this.submitLoading = true
            const params = this.$refs['form'].getFormData()
            await createApi(params)
            this.$message({ type: 'success', message: '添加成功' })
            this.$emit('success')
          } catch (err) {
            console.error(err)
          }
          this.submitLoading = false
        }
      })
    },
    cancel () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../../styles/form.less';
</style>